import fetch from 'node-fetch';

// const options = {
//   method: 'POST',
//   headers: {
//     Authorization: 'Bearer sk-aheubhovuzplwwfjnldcubjxgxwfnwzctlxizkxayllcrjrj',
//     'Content-Type': 'application/json'
//   },
//   body: '{"model":"deepseek-ai/DeepSeek-R1","messages":[{"role":"user","content":"我叫苏德宏，请帮我用画面联想法介绍我的名字写一个150字的自我介绍"}]}'
// };

// fetch('https://api.siliconflow.cn/v1/chat/completions', options)
//   .then(response => response.json())
//   .then(response => console.log(response.choices[0].message.content))
//   .catch(err => console.error(err));


const options = {
  method: 'POST',
  headers: {
    Authorization: 'Bearer sk-aheubhovuzplwwfjnldcubjxgxwfnwzctlxizkxayllcrjrj',
    'Content-Type': 'application/json'
  },
  body: '{"model":"deepseek-ai/DeepSeek-R1-Distill-Llama-70B","messages":[{"role":"user","content":"什么是V字叙事结构，请举个例子说明"}],"stream":true}'
};

async function main() {
  const response = await fetch('https://api.siliconflow.cn/v1/chat/completions', options);
   // 发起请求
   if (!response.ok) {
       throw new Error(`HTTP error! status: ${response.status}`);
   }

   // 直接使用 Node.js 的 Readable 流
   response.body.on('data', (chunk) => {
    // 将buffer转换为字符串
    const str = chunk.toString();
    // 将字符串转换为json
    const json = JSON.parse(str);
    // 输出json
    console.log(json);
       // 将数据块输出到终端
      //  console.log(chunk);
      //  process.stdout.write(chunk.toString());
      // const json = JSON.parse(chunk.toString());
      // // console.log(json.choices[0].delta.content);
      //  process.stdout.write(json.choices?.[0]?.delta?.content);
      
   });

   response.body.on('end', () => {
       console.log('\nStream finished.');
   });

   response.body.on('error', (error) => {
       console.error('Stream error:', error);
   });
}

main();

